/*
 * ConnectFrame.java
 *
 * Created on January 29, 2008, 1:38 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package simulatie;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

import statter.Stats;

/**
 * Het ConnectFrame waarmee de Simulator en de Controller met elkaar kunnen connecten
 * @author stefantalen
 */
public class ConnectFrame extends JFrame implements ActionListener {
  private static final long serialVersionUID = 1L;
  private JButton btn_client;
  private JTextField txt_port;
  private JTextField txt_address;
  private JTextPane txp_pane;
  private JScrollPane sPanel;
  private Stats statter;
private JButton btnStats;
  
  /** Creates a new instance of ConnectFrame */
  public ConnectFrame (Stats statter) {
    super("Connect to...");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    btnStats = new JButton("Stats");
    btn_client = new JButton("Connect");
    txt_port = new JTextField(3);
    txt_address = new JTextField(10);
    txp_pane = new JTextPane();
    txp_pane.setFont(new Font("Verdana", Font.PLAIN, 14));
    txp_pane.setForeground(Color.WHITE);
    txp_pane.setBackground(Color.BLACK);
    txp_pane.setEditable(false);
    txp_pane.setVisible(true);
    
    sPanel = new JScrollPane(txp_pane);
    sPanel.setBorder(new TitledBorder(new EtchedBorder(), "Process",
			  	TitledBorder.DEFAULT_JUSTIFICATION,
				TitledBorder.DEFAULT_POSITION, null, new Color(0, 128, 255)));
    sPanel.setPreferredSize(new Dimension(300, 200));
    
    this.setLayout(new FlowLayout());
    this.add(txt_address);
    this.add(txt_port);
    this.add(btn_client);
    this.add(btnStats);
    this.add (sPanel);
    
    this.pack();
    this.setSize(300, 300);
    this.setVisible(true);
    btn_client.addActionListener(this);
    btnStats.addActionListener(this);
    this.statter = statter;
  }
 /**
  * Plaats een regel op de textpanel
  * @param msg
  */ 
  private void append(String msg) {
    txp_pane.setText(txp_pane.getText() + msg + "\n");
  }

/**
 * Open het Simulatiescherm en maak de connectie
 */  
  public void actionPerformed (ActionEvent e) {
	  if (e.getSource() == btn_client) {
		  statter.addSection("Setting up world");
		  statter.statStart("Setting up world");
		  World world = new World(statter);
		  statter.statEnd("Setting up world");
		  statter.addSection("Connecting to server");
		  statter.statStart("Connecting to server");
		  world.connectServer (txt_address.getText (), Integer.parseInt(txt_port.getText ()));
		  statter.statEnd("Connecting to server");
		  statter.addSection("Graphics loop");
		  append("Connectie met " + txt_address.getText());
	  }
	  else if (e.getSource() == btnStats) {
		  statter.printGlobalStats();
	  }
  }
  
}
